package yz.cp.back.websocket.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import yz.cp.back.common.res.ApiResult;
import yz.cp.back.websocket.domain.OutMessage;

@Controller
@Slf4j
public class WebsocketController {
    @Autowired
    private SimpMessagingTemplate template;
    @GetMapping("/web/open/app/broadcast")
    @ResponseBody
    public ApiResult broadcast() throws Exception {
        System.out.println();
        template.convertAndSend("/topic/handle", OutMessage.builder().content("this is a test").username("all").build());
        return ApiResult.Custom().success();
    }
    @GetMapping("/web/open/app/single")
    @ResponseBody
    public ApiResult single(String name) throws Exception {
        template.convertAndSendToUser(name,"/queue/handle",OutMessage.builder().content("this is a single").username(name).build());
        return ApiResult.Custom().success();
    }
}
